package com.spzx.channel.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.channel.service.IndexService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductSkuService;
import com.spzx.product.api.domain.Category;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.dto.IndexHotData;
import com.spzx.product.api.domain.dto.ProductSkuDTO;
import com.spzx.product.api.domain.dto.SkuSearchDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    RemoteProductSkuService remoteProductSkuService;

    @Resource
    RemoteCategoryService remoteCategoryService;

    @Override
    public IndexHotData getIndexData() {
        //openfeign远程调用时 如果没有指定data的泛型  默认会转为linkedHashMap
        R<IndexHotData> r = remoteProductSkuService.indexHotData();
        if (r.getCode()!=200){
            throw new ServiceException(r.getMsg());
        }
        return r.getData();
    }

    @Override
    public List<Category> listCatesTree() {
        R<List<Category>> r = remoteCategoryService.listCatesTree();
        if (r.getCode() != 200) {
            throw new ServiceException(r.getMsg());
        }
        return r.getData();
    }

    @Override
    public Page<ProductSku> skuList(SkuSearchDTO skuSearchDTO) {
       R<Page<ProductSku>> r = remoteProductSkuService.list(skuSearchDTO);
        if(r.getCode()!=200){
            throw new ServiceException(r.getMsg());
        }
        return r.getData();
    }

    @Override
    public ProductSkuDTO getSkuDetails(Long skuId) {
        R<ProductSkuDTO> r = remoteProductSkuService.getSkuDTOById(skuId);
        if(r.getCode()!=200){
            throw new ServiceException(r.getMsg());
        }
        return r.getData();
    }
}
